package com.example.yblin.hello24;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.yblin.hello24.DataUtils.RanklistDataUtil;

import java.util.List;


public class RanklistAdapter extends RecyclerView.Adapter<RanklistAdapter.RanklistViewHolder> {

    private List<RanklistDataUtil.RankItemData> ranklistData;
    private int mode = RanklistDataUtil.MODE_STANDARD;

    public void setRanklistData(List<RanklistDataUtil.RankItemData> ranklistData, int mode) {

        this.ranklistData = ranklistData;
        this.mode = mode;
        notifyDataSetChanged();
    }

    public RanklistAdapter() {


    }




    @NonNull
    @Override
    public RanklistViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        Context context = parent.getContext();


        LayoutInflater layoutInflater = LayoutInflater.from(context);

        View view = layoutInflater.inflate(R.layout.item_ranklist, parent, false);
        return new RanklistViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull RanklistViewHolder holder, int position) {
        if (mode == RanklistDataUtil.MODE_STANDARD) {
            if (position == 0) {
                holder.tvID.setText("排名");
                holder.tvName.setText("用户名");
                holder.tvRate.setText("正确率");
                holder.tvCostTime.setText("最短用时");
            } else {
                final RanklistDataUtil.RankItemData data = ranklistData.get(position - 1);
                holder.tvID.setText(data.getRank() + "");
                holder.tvName.setText(data.getUsername());
                holder.tvRate.setText((int) (Double.parseDouble(data.getRate()) * 100) + "%");
                holder.tvCostTime.setText(data.getCostTime() + "");
            }
        } else {
            if (position == 0) {
                holder.tvID.setText("排名");
                holder.tvName.setText("用户名");
                holder.tvRate.setText("闯关数");
                holder.tvCostTime.setText("");
            } else {
                final RanklistDataUtil.RankItemData data = ranklistData.get(position - 1);
                holder.tvID.setText(data.getRank() + "");
                holder.tvName.setText(data.getUsername() + "");
                holder.tvRate.setText(data.getStage() + "");
                holder.tvCostTime.setText("");
            }
        }

    }

    @Override
    public int getItemCount() {
        if (ranklistData == null) {
            return 1;
        } else {
            return ranklistData.size() + 1;
        }
    }

    public class RanklistViewHolder extends RecyclerView.ViewHolder {
        public final TextView tvID;
        public final TextView tvName;
        public final TextView tvRate;
        public final TextView tvCostTime;

        public RanklistViewHolder(View itemView) {
            super(itemView);
            tvID = itemView.findViewById(R.id.textView_rank_list_id);
            tvName = itemView.findViewById(R.id.textView_rank_list_name);
            tvRate = itemView.findViewById(R.id.textView_rank_list_rate);
            tvCostTime = itemView.findViewById(R.id.textView_rank_list_cost_time);
        }
    }
}
